package it.infodea.tapestrydea.services.jcr.impl;

import it.infodea.tapestrydea.services.jcr.JcrSessionProviderService;
import it.infodea.tapestrydea.services.jcr.RepositoryAccessService;
import it.infodea.tapestrydea.state.JcrUserSessionState;

import javax.jcr.Credentials;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;

import org.apache.tapestry5.services.ApplicationStateManager;

public class JcrSessionProviderServiceImpl implements JcrSessionProviderService {

	private ApplicationStateManager applicationStateManager;
	private RepositoryAccessService repositoryAccessService;

	public JcrSessionProviderServiceImpl(ApplicationStateManager applicationStateManager,
			RepositoryAccessService repositoryAccessService) {
		this.applicationStateManager = applicationStateManager;
		this.repositoryAccessService = repositoryAccessService;
		
	}

	public Session getSession() {
//		SystemUserDTO systemUserDto = getSystemUserDto();
//		return getSession(systemUserDto.getUsername(), systemUserDto.getPassword());
		return getSession("system", "system");
	}

	public Session getSession(String workspace) {
//		SystemUserDTO systemUserDto = getSystemUserDto();
//		return getSession(systemUserDto.getUsername(), systemUserDto.getPassword(), workspace);
		return getSession("system", "system", workspace);
	}

	public Session getSession(String user, String password) {
		return getSession(user, password, null);
	}

	public Session getSession(String user, String password, String workspace) {
		Session session = null;
		if (user != null){
			SimpleCredentials credentials = new SimpleCredentials(user, password.toCharArray());
			credentials.setAttribute("userHomePath", "/");
			session = getSession(credentials, workspace);	
		}	
		return session;
	}

	public Session getSession(Credentials credentials) {
		return getSession(credentials, null);
	}

	public Session getSession(Credentials credentials, String workspace) {
		Repository repository = repositoryAccessService.getRepository();
		return getSession(repository, credentials, workspace);
	}

	private Session getSession(Repository repository, Credentials credentials, String workspace) {
		JcrUserSessionState jcrUserSessionState = applicationStateManager.get(JcrUserSessionState.class);
		Session session = jcrUserSessionState.getSession();
		if(repository != null && session == null){
			try {
				if(workspace != null){
					session = repository.login(credentials, workspace);
				}else{
					session = repository.login(credentials);
				}
				jcrUserSessionState.setJcrSession(session);
			} catch (RepositoryException e) {
				System.out.println("Errore durante il caricamento del repository");
				e.printStackTrace();
			}
		}
		return session;
	}
	
//	private SystemUserDTO getSystemUserDto() {
//		UserState userState = applicationStateManager.get(UserState.class);
////		userState.getUser().setUsername("system");
////		userState.getUser().setPassword("system");
//		return userState.getUser();
//	}

}
